<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html>
<html>
<head>
<jsp:include page="../inc.jsp"></jsp:include>
<meta http-equiv="X-UA-Compatible" content="edge" />
<c:if test="${fn:contains(sessionInfo.resourceList, '/role/edit')}">
	<script type="text/javascript">
		$.canEdit = true;
	</script>
</c:if>
<c:if test="${fn:contains(sessionInfo.resourceList, '/role/delete')}">
	<script type="text/javascript">
		$.canDelete = true;
	</script>
</c:if>
<c:if test="${fn:contains(sessionInfo.resourceList, '/role/grant')}">
	<script type="text/javascript">
		$.canGrant = true;
	</script>
</c:if>
<title>新闻管理</title>
	<script type="text/javascript">
	var dataGrid;
	var cateTree;
	var categoryid;
	$(function() {
		cateTree = $('#cateTree').tree({
			url : '${ctx}/cate/tree',
			parentField : 'pid',
			lines : true,
			onClick : function(node) {
				categoryid = node.id;
				dataGrid.datagrid('load', {
					categoryId: node.id
				});
			}
		});
		dataGrid = $('#dataGrid').datagrid({
			width:'auto',
			height:'auto',
			url : '${ctx}' + '/news/dataGrid',
			striped : true,
			rownumbers : true,
			pagination : true,
			
			singleSelect : false,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'asc',
			pageSize : 10,
			pageList : [ 5, 10, 20, 30, 50, 100 ],
			frozenColumns : [ [ {
				checkbox:'true',
				field : 'ck'
			}] ],
			columns : [ [{
				width : '350',
				title : '标题',
				field : 'title',
				sortable : true
			} , {
				width : '80',
				title : '栏目',
				field : 'categoryName',
				sortable : true
			}, {
				width : '80',
				title : '编辑',
				field : 'publishedByName'
			} ,{
				width : '130',
				title : '创建时间',
				field : 'pubtime'
			} ,{
				width : '80',
				title : '权重',
				field : 'weight'
			},{
				width : '80',
				title : '点击量',
				field : 'pv'
			} ] ],
			toolbar : '#toolbar'
		});
	});
	
	function addFun() {
		parent.$('#index_tabs').tabs('add',{
			title:'新闻添加',
		    closable: true,
		    href : '${ctx}/news/addNews'

		});
	}
	function recycleFun(id) {
		var ids ="";
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			
			for(var i=0;i<rows.length;i++){
				ids= ids + rows[i].id+"|";
			}
		} else {//点击操作里面的回收站图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		
		parent.$.messager.confirm('询问', '您是否要移动当前所选新闻到回收站？', function(b) {
			if (b) {
					progressLoad();
					$.post('${ctx}/news/recycle', {
						ids : ids
					}, function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
							dataGrid.datagrid('reload');
						}
						progressClose();
					}, 'JSON');
				
			}
		});
	}
	function deleteFun(id) {
		var ids ="";
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			
			for(var i=0;i<rows.length;i++){
				ids= ids + rows[i].id+"|";
			}
		} else {//点击操作里面的删除图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.messager.confirm('询问', '您是否要删除当前所选新闻？', function(b) {
			if (b) {
					progressLoad();
					$.post('${ctx}/news/delete', {
						ids : ids
					}, function(result) {
						if (result.success) {
							parent.$.messager.alert('提示', result.msg, 'info');
							dataGrid.datagrid('reload');
						}
						progressClose();
					}, 'JSON');
				
			}
		});
	}
	function moveFun(id) {
		var ids ="";
		if (id == undefined) {//点击右键菜单才会触发这个
			var rows = dataGrid.datagrid('getSelections');
			
			for(var i=0;i<rows.length;i++){
				ids= ids + rows[i].id+"|";
			}
		} else {//点击操作里面的移动图标会触发这个
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$.modalDialog({
			title : '移动',
			width : 300,
			height : 150,
			href : '${ctx}/news/movePage?ids='+ids,
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_dataGrid = dataGrid;//因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
					var f = parent.$.modalDialog.handler.find('#newsMoveForm');
					f.submit();
				}
			} ]
		});
	}
	function editFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			if(rows.length<1){
				alert("请选择一条新闻进行修改！");
				return false;
				}
			if(rows.length>1){
				alert("请选择单条新闻进行修改！");
				return false;
				}
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		parent.$('#index_tabs').tabs('add',{
			title:'新闻编辑',
		    closable: true,
		    href : '${ctx}/news/editNews?id=' + id

		});
		
	}
	
	function grantFun(id) {
		if (id == undefined) {
			var rows = dataGrid.datagrid('getSelections');
			id = rows[0].id;
		} else {
			dataGrid.datagrid('unselectAll').datagrid('uncheckAll');
		}
		
		
	}
	function searchFun() {
		dataGrid.datagrid('load', $.serializeObject($('#searchForm')));
	}
	function viewFun() {
		alert(categoryid);
	}
	function buildFun() {
		alert(categoryid);
	}
	function cleanFun() {
		$('#searchForm input').val('');
		dataGrid.datagrid('load', {});
	}
	</script>
	
</head>
<body class="easyui-layout" data-options="fit:true,border:false">
		
	<div data-options="region:'center',border:false,title:'新闻列表'">
	
		<table id="dataGrid" data-options=""></table>
	</div>
	<div data-options="region:'west',border:false,split:true,title:'栏目'"  style="width:150px;overflow: hidden; ">
		<ul id="cateTree"  style="width:150px;margin: 10px 10px 10px 10px">
		</ul>
	</div>
	<div id="toolbar" style="display: none;">
	<form id="searchForm">
			<table>
				<tr>
					<th>标题:</th>
					<td><input name="title" placeholder="请输入标题"/></td>
					<th>编辑:</th>
					<td><input name="publishedByName" placeholder="请输入编辑"/></td>
					<th>创建时间:</th>
					<td><input name="createdatetimeStart" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" />至<input  name="createdatetimeEnd" placeholder="点击选择时间" onclick="WdatePicker({readOnly:true,dateFmt:'yyyy-MM-dd HH:mm:ss'})" readonly="readonly" /></td>
					<th>状态:</th>
					<td>
						<select name="status" class="easyui-combobox" data-options="editable:false,panelHeight:'auto'">
							<option value="0" selected="selected">已发布</option>
							<option value="1">草稿</option>
							<option value="2">回收站</option>
						</select>
					</td>
					<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon_search',plain:true" onclick="searchFun();">查询</a><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon_cancel',plain:true" onclick="cleanFun();">清空</a></td>
				</tr>
			</table>
		</form>
		<c:if test="${fn:contains(sessionInfo.resourceList, '/news/add')}">
			<a onclick="addFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_add'">添加</a>
		</c:if>
		<c:if test="${fn:contains(sessionInfo.resourceList, '/news/edit')}">
			<a onclick="editFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_edit'">修改</a>
		</c:if>
		<c:if test="${fn:contains(sessionInfo.resourceList, '/news/recycle')}">
			<a onclick="recycleFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_recycle'">放入回收站</a>
		</c:if>
		<c:if test="${fn:contains(sessionInfo.resourceList, '/news/delete')}">
			<a onclick="deleteFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_del'">彻底删除</a>
		</c:if>
		<c:if test="${fn:contains(sessionInfo.resourceList, '/news/move')}">
			<a onclick="moveFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_move'">批量移动</a>
		</c:if>
		<span style="float:right;padding-top:1px;margin-right:30px;">
			<a onclick="viewFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_cate'">查看栏目页</a>
			<a onclick="buildFun();" href="javascript:void(0);" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon_build'">生成栏目页</a>
		</span>
	</div>
</body>
</html>